package com.yunkaweilai.android.net;


import com.yunkaweilai.android.base.BaseApplication;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.https.HttpsUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Response;

/**
 * 网络请求的封装
 * 使用实例：HttpRequest.getInstance().post().addParams("d","d").execute(new StringCallback());
 * Created by Administrator on 2017-02-22.
 */

public class HttpRequest {
    private volatile static HttpRequest mInstance;

    private HttpRequest() {
        /*HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
*/
        //设置可访问所有的https网站
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
        final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                //.addInterceptor(new LoggerInterceptor("net"))
                .connectTimeout(90, TimeUnit.SECONDS)
                .writeTimeout(45,TimeUnit.SECONDS)
                .readTimeout(45,TimeUnit.SECONDS)
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .build();

        OkHttpUtils.initClient(okHttpClient);

    }

    public static HttpRequest initClient() {
        if (mInstance == null) {
            synchronized (HttpRequest.class) {
                if (mInstance == null) {
                    mInstance = new HttpRequest();
                }
            }
        }
        return mInstance;
    }

    /**
     * post请求
     *
     * @return
     */
    public static PostProcess post() {
        return new PostProcess();
    }

    public static PostProcess postUrl(String url) {
        return new PostProcess(url);
    }
    /**
     * 同步下载文件
     *
     * @param url
     */
    public static Response download(String url) {
        try {
            return OkHttpUtils
                    .get()
                    .url(url)
                    .build()
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }





}
